package com.powerhousecreative.q;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.text.util.Linkify;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

public class YoutubeDetailActivity extends Activity {
	private JSONObject entry;
	private WebView embedView;
	private String videoID;
	final Handler handler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_youtube_detail);
		embedView = (WebView) findViewById(R.id.youtubeEmbedView);
		embedView.setWebChromeClient(new WebChromeClient());
		
		WebSettings setting = embedView.getSettings();
		setting.setDefaultTextEncodingName("utf-8");
		setting.setJavaScriptEnabled(true);		
		setting.setLoadsImagesAutomatically(true);
		setting.setPluginState(WebSettings.PluginState.ON);
		
		try {
			entry = new JSONObject(getIntent().getStringExtra("entry"));			
			videoID = entry.getJSONObject("snippet").getJSONObject("resourceId").getString("videoId");
			
			TextView titleText = (TextView) findViewById(R.id.titleText1);
			titleText.setText(entry.getJSONObject("snippet").getString("title"));
			
			TextView descText = (TextView) findViewById(R.id.descText1);
			descText.setText(entry.getJSONObject("snippet").getString("description"));
			descText.setMovementMethod(new ScrollingMovementMethod());
			Linkify.addLinks(descText, Linkify.ALL);
			
			handler.postDelayed(new Runnable() {
				@Override
				public void run() {
					String html = "<html><head><style type=\"text/css\">" +
								"body { background-color: transparent;" +
								"color: white;}	</style>" +
								"</head><body style=\"margin:0\">" +
								"<iframe src=\"http://www.youtube.com/embed/" + videoID + "\"" +
								"width=\"" + 0.75f * embedView.getWidth() + "\" height=\"" + 0.75f * embedView.getHeight() + "\" allowfullscreen frameborder=\"0\"></iframe></body></html>";
			
					embedView.loadDataWithBaseURL("http://www.youtube.com", html, "text/html", "UTF-8", null);					
				}	
			}, 100);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event)
//	{
//	    if ((keyCode == KeyEvent.KEYCODE_BACK))
//	    {
//	        finish();
//	    }
//	    return super.onKeyDown(keyCode, event);
//	}
	
	@Override
	public void onPause() {
	    super.onPause();

	    try {
	        Class.forName("android.webkit.WebView")
	                .getMethod("onPause", (Class[]) null)
	                            .invoke(embedView, (Object[]) null);

	    } catch(ClassNotFoundException cnfe) {
	        
	    } catch(NoSuchMethodException nsme) {
	        
	    } catch(InvocationTargetException ite) {
	        
	    } catch (IllegalAccessException iae) {
	        
	    }
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.youtube_detail, menu);
		return true;
	}
	
	// Remove duplicate string
	public List<String> remvoeduplicateStringFromList (List<String> list, boolean ignoreCase) {
		HashSet<String> occur = new HashSet<String> ();
		ArrayList<String> returnList = new ArrayList<String> ();
		
		for (String string : list) {
			if (occur.add(ignoreCase ? string.toLowerCase() : string)) {
				returnList.add(string);
			}
		}
		return returnList;
	}

}
